package cn.com.bluemoon.daps.api.sys.factory;

import cn.com.bluemoon.daps.api.sys.RemoteSystemDatasourceService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiSystem;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.toolkit.FeignClientTools;
import cn.com.bluemoon.daps.system.dto.DatasourceDto;
import cn.com.bluemoon.daps.system.dto.SystemDatasourceDto;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasource;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasourceEnvironment;
import cn.com.bluemoon.daps.system.entity.DapSystemSchema;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Component
public class RemoteSystemDatasourceServiceFallbackFactory implements FallbackFactory<RemoteSystemDatasourceService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteSystemDatasourceServiceFallbackFactory.class);

    @Override
    public RemoteSystemDatasourceService create(Throwable throwable) {
        log.error("系统数据源服务调用失败:{}", throwable.getMessage());
        return new RemoteSystemDatasourceService() {

            @Override
            public ResultBean<DapSystemDatasource> getDatasource(String id) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public DapSystemDatasource getById(String id) {
                return null;
            }

            @Override
            public ResultBean<List<DatasourceDto>> getDatasourceList() {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<List<DatasourceDto>> datasourceList(List<String> ids) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public  ResultBean<DatasourceDto> getDatasourceInfo(String id) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<Boolean> isExistPreAndProEnvironment(String datasourceId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<List<SystemDatasourceDto>> getResourceBySystemId(@RequestBody List<String> systemIdList) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<List<DapSystemDatasource>> getDatasourceByDatabaseType(DatabaseType databaseType) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<Map<String, List<DapSystemDatasourceEnvironment>>> getSchemaDatasourceList(String schemaId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

            @Override
            public ResultBean<List<DapSystemSchema>> getSchemaList() {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiSystem.PROD_SERVICE, "获取数据源信息失败");
            }

        };
    }
}
